﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=397704
// To debug code on page load in Ripple or on Android devices/emulators: launch your app, set breakpoints, 
// and then run "window.location.reload()" in the JavaScript Console.
(function () {
    "use strict";

    document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

    function onDeviceReady() {
        // Handle the Cordova pause and resume events
        document.addEventListener( 'pause', onPause.bind( this ), false );
        document.addEventListener( 'resume', onResume.bind( this ), false );
        
    };

    function onPause() {
        // TODO: This application has been suspended. Save application state here.
    };

    function onResume() {
        // TODO: This application has been reactivated. Restore application state here.
    };
})();

function logIn() {
    var password = $("#password").val();
    var login = $("#login").val();
    if(!password || !login) {
        return;
    }
    var apiUrl = localStorage.getItem("baseUrl");
    $.ajax({
        url: apiUrl + "api/accountapi",
        type: "POST",
        data: { login: login, password: password },
        error: function () {
            alert("Ошибка запроса к серверу. Пожалуйста, попробуйте еще раз");
            $("#logBtn").removeAttr("disabled");
            $(".loader").hide();
        },
        beforeSend: function() {
            $("#logBtn").attr("disabled", "true");
            $(".loader").show();
        },
        success: function (response) {
            if (!response) {
                alert("Ошибка. Сервер недоступен");
            } else {
                if (response.error) {
                    alert("Неверный логин или пароль");
                } else {
                    localStorage.setItem("userLogin", login)
                    window.location = "profile.html";
                }
            }
            $("#logBtn").removeAttr("disabled");
            $(".loader").hide();
        }
    });
}